package org.demo.eventbus;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.demo.datasoureces.entity.UserInfo;
import org.demo.datasoureces.service.UserInfoService;
import org.demo.redis.entity.User;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * guava 事件总线示例
 */
@Slf4j
public class EventBusDemo {
    @Autowired
    UserInfoService userInfoService;

    static EventBus bus = new EventBus();
    static {
        bus.register(new EventBusDemo());
    }

    public static void eventDemo(User user){
        log.info("user:{}",user);
        bus.post(new DemoEvent(user));
    }

    @Subscribe
    public void handle(DemoEvent ae){
        User user = ae.getUser();
        log.info("rec user is :{}",user);
        log.info("rec userid is :{}",user.getId());
        UserInfo userInfo = userInfoService.queryUserInfoById(user.getId());
        log.info("USERIINFO IS:{}",userInfo);
    }
}
